<%@ include file="header.jsp"%>

<style type="text/css">
 body {width:100%; height:100%; overflow:hidden, margin:0}
 html {width:100%; height:100%; overflow:hidden}
 
.x-column-header-text {
	white-space: normal !important;
}

.x-grid3-cell-inner, .x-grid3-hd-inner { white-space:normal !important; } 
</style>

<table align="center" cellpadding="0" cellspacing="0" height="100%"
	width="100%" border="0">
	<tr>
		<td valign="top">
		<div id="pipelineDiv" />
		</td>
	</tr>
</table>

<script type="text/javascript">
Ext.require(['Ext.data.*', 'Ext.grid.*']);

var pipelineGrid, statusWin;

function changeStatus(orderId,clientUniqueId){
	if(!statusWin){
		var statusForm = Ext.create('Ext.form.Panel', {
			frame: true,
			bodyStyle:'padding:5px 5px 0',
			forceFit:true,
			items:[{
				xtype:'combo',
				fieldLabel:'New Status',
				id:'newStatus',
				queryMode: 'local',
				labelWidth:100,
				width:220,
				valueField: 'value',
				displayField: 'text', 
				store: new Ext.create('Ext.data.Store', {
					fields: ['value', 'text'],
					data: [
						{"value":"-3", "text":"Canceled"},
						{"value":"3", "text":"Pending(Green Status)"},
						{"value":"4", "text":"Green Status"},
						{"value":"13", "text":"Completed"}
					]
				})
			}]
		});
		statusWin = Ext.create('Ext.Window', {
			header:false,
			border:0,
			modal:true,
			buttonAlign : "center",
			width:300,
			height:80,
			defaults: {
				anchor: '100%'
			},
			items: [statusForm],
			closeAction: function(){
				statusWin.hide();
			},
			buttons:[{
				text: 'OK',
				handler: function() {
					var newStatus = Ext.getCmp('newStatus').getValue();
					if(newStatus){
						var pipelineGrid0 = Ext.getCmp('pipelineGrid');
						pipelineGrid0.getEl().mask('processing...');
						Ext.Ajax.request({
							url: 'UpdateScheduleStatus.action',
							params: {
								orderId:orderId,
								clientUniqueId:clientUniqueId,
								newStatus:newStatus
							},
							success: function(response){
								var result = Ext.util.JSON.decode(response.responseText);
								Ext.Msg.alert('Info', 'status changed.');
								var store0 = pipelineGrid0.getStore();
								var record0;
								for(var ii=0;ii<store0.getCount();ii++){
									record0 = store0.getAt(ii);
									if(record0.data.orderId==orderId&&record0.data.clientUniqueId==clientUniqueId){
										record0.data.status = Ext.getCmp('newStatus').getRawValue();
										pipelineGrid0.getView().refresh();
										break;
									}
								}
								pipelineGrid0.getEl().unmask();
							},
							failure: function(response){
								Ext.Msg.alert('Warning', response.responseText);
								pipelineGrid0.getEl().unmask();
							}
						});
						statusWin.hide();
					}else{
						Ext.Msg.alert('Warning', 'Please select a new status!');
					}
				}
			},{
				text: 'Cancel',
				handler: function() {
					statusForm.getForm().reset();
					statusWin.hide();
				}
			}]
		});
	}
	statusWin.show();
}

function validateRecords(records,modelId){
	var ret = '';
	if(modelId==12){
		for(var i=0;i<records.length;i++){
			var record = records[i];
			if(record.data.fixture=='' || record.data.duration=='' || record.data.outSourcing=='' || record.data.testLocation==''){
				validate = false;
				ret = 'Please fill in Fixture/Duration/OutSourcing/TestLocation for record['+(pipelineGrid.getStore().indexOf(record)+1)+'];'
				break;
			}
		}	
	}
	return ret;
}

function saveSchedule(){
	var modelId = getURLParameter('modelId');
	/*var result = validateRecords(records,modelId);
	if(result!=''){
		Ext.Msg.alert('Warning', result);
		return;
	}*/
	var params,pipelineGrid0 = Ext.getCmp('pipelineGrid');
	if(modelId==12){
		var requestNModels='',fixture='',duration='',outSourcing='',testLocation='';
		var records = pipelineGrid0.getStore().getUpdatedRecords();
		for(var i=0;i<records.length;i++){
			var record = records[i];
			requestNModels += record.data.orderId + '-' + record.data.clientUniqueId;
			fixture += record.data.fixture;
			duration += record.data.duration;
			outSourcing += record.data.outSourcing;
			testLocation += record.data.testLocation;
			if(i<records.length-1){
				requestNModels += ',';
				fixture += ',';
				duration += ',';
				outSourcing += ',';
				testLocation += ',';
			}
		}
		params={
			requestNModels: requestNModels,
			fixture:fixture,
			outSourcing:outSourcing,
			testLocation:testLocation,
			duration:duration
		};
		Ext.Ajax.request({
			url: 'saveSchedule.action',
			params: params,
			success: function(response){
				Ext.MessageBox.alert('Info', 'Operation completed.');
			},
			failure: function(response){
				Ext.MessageBox.alert('Warning', 'Operation failed,please try again later.');
			}
		});
	}
}

Ext.onReady(function(){
	
	Ext.create('Ext.panel.Panel',{
		renderTo:'pipelineDiv',
		layout: 'fit',
		autoScroll:false,
		items:[{
			xtype:'panel',
			forceFit:true,
			layout: {
				type: 'table',
				columns: 1,
				tableAttrs: {
					border:0,
					width:'100%'
				}
			},
			id:'pipelinePnl',
			items:[{
				xtype:'panel',
				layout:'hbox',
				frame:true,
				items:[{
					xtype:'combo',
					id:'cmbModel',
					fieldLabel:'Choose Model',
					queryMode: 'local',
					displayField: 'name',
					valueField: 'id',
					store:new Ext.create('Ext.data.Store', {
						fields: ['name','id'],
						data:[{name:'static deployment',id:12},{name:'Linear Impact',id:3}]
					})
				},/*{
					xtype:'tbspacer',
					width: 90
				},{
					xtype:'checkbox',
					id:'chkboxShowSchedule',
					fieldLabel:'Show Only Schedulable',
					labelWidth:150,
					handler:function(){
						var pipelineGrid0 = Ext.getCmp('pipelineGrid');
						if(this.getValue()&&pipelineGrid0){
							pipelineGrid0.getStore().filterBy(function(record,recordId){
								return record.data.status=='Green Status';
							});
						}else{
							pipelineGrid0.getStore().clearFilter();
						}
					}
				},*/{
					xtype:'tbspacer',
					width: 50
				},{
					xtype:'combo',
					fieldLabel:'Status',
					id:'txtStatus',
					queryMode: 'local',
					labelWidth:50,
					width:220,
					valueField: 'value',
					displayField: 'text', 
					store: new Ext.create('Ext.data.Store', {
						fields: ['value', 'text'],
						data: [
							{"value":"-999", "text":"--All--"},
							{"value":"-2", "text":"Reject"},
							{"value":"0", "text":"Unfinished"},
							{"value":"102", "text":"Pending(Pipeline)"},
							{"value":"2", "text":"Pipeline"},
							{"value":"103", "text":"Pending Sample(Pipeline)"},
							{"value":"3", "text":"Pending(Green Status)"},
							{"value":"4", "text":"Green Status"},
							{"value":"13", "text":"Completed"}
						]
					}),
					value:"-999"
				},{
					xtype:'tbspacer',
					width: 50
				},{
					xtype:'combo',
					fieldLabel:'Additional Criteria',
					id:'addCriteria',
					queryMode: 'local',
					labelWidth:100,
					width:230,
					valueField: 'value',
					displayField: 'text',
					store: new Ext.create('Ext.data.Store', {
						fields: ['value', 'text'],
						data: [
							{"value":"", "text":"--None--"},
							{"value":"trpNo", "text":"Trp No."},
							{"value":"prjNum", "text":"Project Number"},
							{"value":"prj", "text":"Project"},
							{"value":"prd", "text":"Product"}
						]
					}),
					value:""
				},{
					xtype:'textfield',
					id:'addValue',
					width:120
				},{
					xtype: 'button',
					text: 'Search',
					listeners:{
						'click':function(){
							var pipelineGrid0 = Ext.getCmp('pipelineGrid');
							if(pipelineGrid0){
								pipelineGrid0.getEl().mask('loading...');
								pipelineGrid0.getStore().loadPage(1);
							}
						}
					}
				}]
			}]
		}],
		buttonAlign:'center',
		buttons:[{
			text:'Save',
			cls:'big-button',
			handler:saveSchedule
		}]	
	});
	
	var modelId = getURLParameter('modelId');
	var cmp = Ext.getCmp('cmbModel');
	if(cmp){
		var st = cmp.getStore();
		for(var i=0;i<st.getCount();i++){
			if(st.getAt(i).data.id==modelId){
				cmp.setValue(st.getAt(i));
				loadJS('jsp/pipeline/'+modelId+'.js');
				break;
			}
		}
		cmp.on('change',function(field,newValue,oldValue,eOpts){
							document.location='viewPipeline.action?modelId='+newValue;
						});
		}
	
	
	
});
</script>

<%@ include file="footer.jsp"%>